package org.jeecg.modules.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.biz.entity.EventExchange;
import org.jeecg.modules.biz.service.EventExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "闪兑")
@RestController
@RequestMapping("/exchange")
public class EventExchangeController {
    @Autowired
    private EventExchangeService exchangeService;

    @ApiOperation("闪兑列表-管理员")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<EventExchange>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             EventExchange eventExchange,
                                             HttpServletRequest request){
        Result<IPage<EventExchange>> result = new Result<>();

        QueryWrapper<EventExchange> queryWrapper =QueryGenerator.initQueryWrapper(eventExchange, request.getParameterMap());;
        queryWrapper.orderByDesc("timestamp");

        Page<EventExchange> page = new Page<>(pageNo, pageSize);
        IPage<EventExchange> pageList = exchangeService.page(page, queryWrapper);
        result.setResult(pageList);

        return result;
    }

    @ApiOperation("日闪兑列表")
    @RequestMapping(value = "/list/day/{address}", method = RequestMethod.GET)
    public Result<List<EventExchange>> dayList4Address(@PathVariable String address){
        Result<List<EventExchange>> result = new Result<>();

        QueryWrapper<EventExchange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("caller", address);
        queryWrapper.eq("exchange_type", "day");
        queryWrapper.orderByDesc("timestamp");

        List<EventExchange> list = exchangeService.list(queryWrapper);
        result.setResult(list);

        return result;
    }

    @ApiOperation("月闪兑列表")
    @RequestMapping(value = "/list/month/{address}", method = RequestMethod.GET)
    public Result<List<EventExchange>> monthList4Address(@PathVariable String address){
        Result<List<EventExchange>> result = new Result<>();

        QueryWrapper<EventExchange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("caller", address);
        queryWrapper.eq("exchange_type", "month");
        queryWrapper.orderByDesc("timestamp");

        List<EventExchange> list = exchangeService.list(queryWrapper);
        result.setResult(list);

        return result;
    }
}
